package chat_server;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Registrator implements Runnable
{
    private Distributor distributor;
    private ServerSocket serverSocket;

    public Registrator(Distributor distributor, ServerSocket serverSocket)
    {
	this.distributor = distributor;
	this.serverSocket = serverSocket;
    }

    @Override public void run()
    {
	while (true)
	{
	    try
	    {
		Socket socket = this.serverSocket.accept();

		Scanner entry = new Scanner(socket.getInputStream());
		PrintStream exit = new PrintStream(socket.getOutputStream());

		Receptor receptor = new Receptor(entry, this.distributor);
		Thread stack = new Thread(receptor);
		stack.start();

		Issuing issuing = new Issuing(exit);

		this.distributor.addIssuing(issuing);
	    }
	    catch (IOException e)
	    {
		System.out.println("Error!");
	    }
	}
    }
}
